//
//  RegisterViewController.m
//  WBBaseProject
//
//  Created by 吴斌 on 2018/3/19.
//  Copyright © 2018年 吴斌. All rights reserved.
//

#import "RegisterViewController.h"
#import "PhoneOrPwdView.h"
#import "GetVerfitCodeView.h"
#import "DeviceInfo.h"
@interface RegisterViewController ()<UITextFieldDelegate>{
    dispatch_source_t _timer;
}

/** 号码 */
@property (nonatomic,strong) PhoneOrPwdView *phoneView;
/** 验证码 */
@property (nonatomic,strong) GetVerfitCodeView *codeView;
/** 密码 */
@property (nonatomic,strong) GetVerfitCodeView *pwdView;

@end

@implementation RegisterViewController

#pragma mark - 生命周期 Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"注册";
    NSLog(@"%@",[DeviceInfo getDeviceInfo]);
    [self setUpUI];
}

- (void)setUpUI
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    scrollView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight + 1);
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerTapped:)];
    [scrollView addGestureRecognizer:singleTap];
    [self.view addSubview:scrollView];
    
    //手机号码
    PhoneOrPwdView *phoneView = [[NSBundle mainBundle] loadNibNamed:@"PhoneOrPwdView" owner:self options:nil].firstObject;
    phoneView.imageIV.image = WB_ImageName(@"用户名-grey");
    phoneView.frame = CGRectMake(0, 100, kScreenWidth, 55);
    phoneView.inputTF.placeholder = @"请输入手机号码";
    phoneView.inputTF.delegate = self;
    phoneView.inputTF.keyboardType = UIKeyboardTypeNumberPad;
    _phoneView = phoneView;
    [scrollView addSubview:phoneView];
    
    //验证码
    GetVerfitCodeView *codeView = [[NSBundle mainBundle] loadNibNamed:@"GetVerfitCodeView" owner:self options:nil].firstObject;
    codeView.imageIV.image = WB_ImageName(@"安全grey");
    codeView.frame = CGRectMake(0, phoneView.mm_bottom, kScreenWidth, 55);
    codeView.inputTF.keyboardType = UIKeyboardTypeNumberPad;
    codeView.inputTF.placeholder = @"请输入验证码";
    codeView.inputTF.delegate = self;
    codeView.input_width.constant = 120.0f;
    [codeView.getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    _codeView = codeView;
    codeView.getCodeBtn.tag = 1004;
    [codeView.getCodeBtn addTarget:self action:@selector(clickMethod:) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:codeView];
   
   //密码
    GetVerfitCodeView *pwdView = [[NSBundle mainBundle] loadNibNamed:@"GetVerfitCodeView" owner:self options:nil].firstObject;
    pwdView.imageIV.image = WB_ImageName(@"密码-grey");
    pwdView.frame = CGRectMake(0, codeView.mm_bottom, kScreenWidth, 55);
    pwdView.inputTF.placeholder = @"请输入6～16为密码(包含字母和数字)";
    pwdView.inputTF.delegate = self;
    pwdView.inputTF.secureTextEntry = YES;
    [pwdView.getCodeBtn setImage:WB_ImageName(@"闭眼") forState:UIControlStateNormal];
    [pwdView.getCodeBtn setImage:WB_ImageName(@"眼睛_open") forState:UIControlStateSelected];
    [pwdView.getCodeBtn addTarget:self action:@selector(clickMethod:) forControlEvents:UIControlEventTouchUpInside];
    pwdView.getCodeBtn.tag = 1002;
    _pwdView = pwdView;
    [scrollView addSubview:pwdView];
    
    //注册
    UIButton *registerBtn = [Factory customButtonWithTitle:@"注册"];
    registerBtn.tag = 1000;
    registerBtn.frame = CGRectMake(35, pwdView.mm_bottom + 50, kScreenWidth - 70, 60);
    [registerBtn addTarget:self action:@selector(clickMethod:) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:registerBtn];
    
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]  style:UIBarButtonItemStylePlain target:self action:@selector(clickMethod:)];
    leftBarItem.tag = 1001;
    self.navigationItem.leftBarButtonItem = leftBarItem;
}

#pragma mark - textField Delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (_phoneView.inputTF == textField) {
        _phoneView.imageIV.image = WB_ImageName(@"用户名_blue");
        _phoneView.lineView.backgroundColor = WB_RGBColor(79, 155, 250);
    }else if (_pwdView.inputTF == textField){
        _pwdView.imageIV.image = WB_ImageName(@"密码_blue");
        _pwdView.lineView.backgroundColor = WB_RGBColor(79, 155, 250);
//        _pwdView.getCodeBtn.hidden = NO;
    }else if (_codeView.inputTF == textField){
        _codeView.imageIV.image = WB_ImageName(@"安全-blue");
        _codeView.lineView.backgroundColor = WB_RGBColor(79, 155, 250);
    }
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (_phoneView.inputTF == textField) {
        _phoneView.imageIV.image = WB_ImageName(@"用户名-grey");
        _phoneView.lineView.backgroundColor = WB_RGBColor(221, 221, 221);
    }else if (_pwdView.inputTF == textField){
        _pwdView.imageIV.image = WB_ImageName(@"密码-grey");
        _pwdView.lineView.backgroundColor = WB_RGBColor(221, 221, 221);
//        _pwdView.getCodeBtn.hidden = YES;
    }else if (_codeView.inputTF == textField){
        _codeView.imageIV.image = WB_ImageName(@"安全grey");
        _codeView.lineView.backgroundColor = WB_RGBColor(221, 221, 221);
    }
}

- (void)clickMethod:(UIButton *)sender
{
    if (sender.tag == 1001) {
        [self.navigationController popViewControllerAnimated:YES];
    }else if (sender.tag == 1002) {
        sender.selected = !sender.selected;
        _pwdView.inputTF.secureTextEntry = !sender.selected;
    }else if (sender.tag == 1000) {
        
        if (WB_IsNilString(_phoneView.inputTF.text)) {
            [NSObject wb_showAlertWithTitle:@"手机号码不能为空"];
            return;
        }else if (![Factory isPhoneNumber:_phoneView.inputTF.text]){
            [NSObject wb_showAlertWithTitle:@"请输入正确的手机号码"];
            return;
        }else if (WB_IsNilString(_pwdView.inputTF.text)){
            [NSObject wb_showAlertWithTitle:@"密码不能为空"];
            return;
        }else if (_pwdView.inputTF.text.length < 6){
            [NSObject wb_showAlertWithTitle:@"密码必须大于或等于6位字符"];
            return;
        }else if (![Factory passwordCheck:_pwdView.inputTF.text]) {
            [NSObject wb_showAlertWithTitle:@"密码请输入字母和数字"];
            return;
        }else if (WB_IsNilString(_codeView.inputTF.text)){
            [NSObject wb_showAlertWithTitle:@"请输入短信验证码"];
            return ;
        }else if (_codeView.inputTF.text.length < 6){
            [NSObject wb_showAlertWithTitle:@"验证码为6位"];
            return;
        }
        
        [XYHttpRequest dataRequestRegisterWithphone:_phoneView.inputTF.text withPassword:_pwdView.inputTF.text withSmsCode:_codeView.inputTF.text success:^(NSDictionary *success) {
            if (WB_IsEquallString(success[@"responseCode"], @"1")) {
                [NSObject wb_showAlertWithTitle:@"注册成功!"];
                WB_DISPATCH_MAIN_Delay_THREAD(1.5, ^{
                    [self.navigationController popViewControllerAnimated:YES];
                })
            }else{
                [NSObject wb_showAlertWithTitle:success[@"info"]];
            }
            
        } failure:^(NSError *error) {
        }];
        
    }else if (sender.tag == 1004) {
        if (WB_IsNilString(_phoneView.inputTF.text)) {
            [NSObject wb_showAlertWithTitle:@"手机号码不能为空"];
            return;
        }else if (![Factory isPhoneNumber:_phoneView.inputTF.text]){
            [NSObject wb_showAlertWithTitle:@"请输入正确的手机号码"];
            return;
        }
        [XYHttpRequest dataRequestGetMessageWithPhone:_phoneView.inputTF.text codeLength:6 success:^(id responseObj) {
            [NSObject wb_showAlertWithTitle:responseObj[@"info"]];
            if (WB_IsEquallString(responseObj[@"responseCode"], @"1")) {
                WB_DISPATCH_MAIN_THREAD(^{
                    _timer = [Factory startTimeWithBtn:_codeView.getCodeBtn];
                });
            }
        } failure:^(NSError *error) {
        }];
    }
}

- (void)fingerTapped:(UITapGestureRecognizer *)gestureRecognizer
{
    UIScrollView *scrollView = (UIScrollView *)gestureRecognizer.view;
    [scrollView endEditing:YES];
}

@end
